//
//  UIImageView+Extension.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/10.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let showPicStrategy : String = "-trend_list_style"

extension UIImageView {
    
    internal func setHeader(url:String?){
        
        let placeHolder = UIImage(named: "test头像")
        
        if let _ = url {
            
            let urlString = url!.lowercaseString
            
            if (url != "" && urlString.hasPrefix("http://")) || (url != "" && urlString.hasPrefix("https://")) {
                
                yy_setImageWithURL(NSURL(string: url!), placeholder: placeHolder, options: YYWebImageOptions(rawValue: YYWebImageOptions.SetImageWithFadeAnimation.rawValue | YYWebImageOptions.ProgressiveBlur.rawValue | YYWebImageOptions.IgnoreFailedURL.rawValue), progress: { (receivedSize, expectedSize) in
                    
                    }, transform: { (image, URL) -> UIImage? in
                        
                        return image.yy_imageByRoundCornerRadius(CGFloat(5))
                        
//                        return image.yy_imageByRoundCornerRadius(max(image.size.height, image.size.width))
                        
                    }, completion: { [weak self](image, URL, from, stage, error) in
                        self!.image = image ?? placeHolder
                    })
                
            } else {
                
                image = placeHolder
            }
        } else {
            
            image = placeHolder
        }

    }
    
}

extension UIImageView {
    
    //
    func loadCoverImage(url: String?) {
        
        let placeHolder = UIImage(named: "相册背景图")
        
        let showUrl = url! + showPicStrategy
        
        let urlString = showUrl.lowercaseString
        
        if (showUrl != "" && urlString.hasPrefix("http://")) || (showUrl != "" && urlString.hasPrefix("https://")) {
            
            yy_setImageWithURL(NSURL(string: showUrl), placeholder: placeHolder, options: YYWebImageOptions(rawValue: YYWebImageOptions.SetImageWithFadeAnimation.rawValue | YYWebImageOptions.ProgressiveBlur.rawValue | YYWebImageOptions.IgnoreFailedURL.rawValue)) { [weak self] (image, url, from, stage, error) in
                self!.image = image ?? placeHolder
            }
        } else {
            
            image = placeHolder
        }
    }
    
    func loadPicImage(url: String) {
        
        let placeHolder = UIImage(named: "相册背景图")
        
        let urlString = url.lowercaseString
        
        if (urlString.hasPrefix("http://")) || (url != "" && urlString.hasPrefix("https://")) {
            
            yy_setImageWithURL(NSURL(string: url), placeholder: placeHolder, options: YYWebImageOptions(rawValue: YYWebImageOptions.SetImageWithFadeAnimation.rawValue | YYWebImageOptions.ProgressiveBlur.rawValue | YYWebImageOptions.IgnoreFailedURL.rawValue)) { [weak self] (image, url, from, stage, error) in
                self!.image = image ?? placeHolder
            }
        } else {
            
//            let im =
            
            image = placeHolder
        }
    }


    
}

